<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user
 *
 * @author David
 */
class user extends APUXIN_SERVICE {
    private $username;
    private $pw;
    private $rights = 0;

    public function __construct()
    {
        $session = load_service("sessions");
        if(!$session->blank("rights"))
        {
            @$this->username = $session->getValue("username");
            @$this->rights = $session->getValue("rights");
        }
        parent::__construct();
        $this->info_setName("user");
        $this->info_setDesc("Stores information about the user");
        $this->info_setVersion(0.7);
        $this->info_setOnlyOnce(true);
    }

    public function dologin($username, $pw)
    {
        $session = load_service("sessions");
        if($this->readuserxml($username))
        {
            if($pw == $this->pw)
            {
                $session->setValue("username", $this->username);
                $session->setValue("rights", $this->rights);
                return true;
            }
        }
    }

    public function getrights()
    {
        return $this->rights;
    }

    public function getusername()
    {
        return $this->username;
    }

    private function readuserxml($username)
    {
        if(file_exists("accounts/$username/user.xml"))
        {
            $file = file_get_contents("accounts/$username/user.xml");
            preg_match("|<name>(.*?)</name>|si", $file, $uname);
            preg_match("|<pw>(.*?)</pw>|si", $file, $pw);
            preg_match("|<rights>(.*?)</rights>|si", $file, $rights);
            $this->username = $uname[1];
            $this->pw = $pw[1];
            $this->rights = $rights[1];
            return true;
        } else
        {
            return false;
        }
    }
}
?>
